﻿using Blog1._0.Data;
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Data.Entity.ModelConfiguration;
using System.Data.SQLite.EF6.Migrations;
using System.Linq;
using System.Reflection;

namespace Blog1._0.Data
{
    /// <summary>
    /// 数据库上下文
    /// </summary>
    public class SqliteDbContext : DbContext, IDbContext
    {
        #region 构造函数


        public SqliteDbContext(): base("name=Guo_Blogs")
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<SqliteDbContext, SysConfiguration>());

        }
        /// <summary>
        /// 初始化一个 使用指定数据连接名称或连接串 的数据访问上下文类 的新实例
        /// </summary>
        /// <param name="connString"></param>
        public SqliteDbContext(string connString)
            : base("name="+ connString)
        {
            //this.Configuration.AutoDetectChangesEnabled = false;//自动跟踪对象的属性变化，默认为true
            //this.Configuration.ValidateOnSaveEnabled = false;//保存前验证对象的属性最大最小长度等，默认为true。
            //this.Configuration.LazyLoadingEnabled = false;//延迟加载，默认为true
            //this.Configuration.ProxyCreationEnabled = false;//追踪
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<SqliteDbContext, SysConfiguration>());

        }
        #endregion

        #region 重载
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            string assembleFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("Blog1.0.Data.DLL", "Blog1.0.Mapping.dll").Replace("file:///", "");
            Assembly asm = Assembly.LoadFile(assembleFileName);
            var typesToRegister = asm.GetTypes()
            .Where(type => !String.IsNullOrEmpty(type.Namespace))
            .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }
            base.OnModelCreating(modelBuilder);

            //配置数据权限(全局过滤)
           // modelBuilder.Filter("BI_E", (BI_E b, int a) => (b == a),() => false);

        }
        #endregion


        //public System.Data.Entity.DbSet<Guo_Type> Guo_Types { get; set; }

        //public System.Data.Entity.DbSet<Guo_Acticle> Guo_Acticles { get; set; }
        //public System.Data.Entity.DbSet<Guo_Catalog> Guo_Catalogs { get; set; }


        


    }
}
public class SysConfiguration : DbMigrationsConfiguration<SqliteDbContext>
{
    public SysConfiguration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
        SetSqlGenerator("System.Data.SQLite", new SQLiteMigrationSqlGenerator());
    }

    protected override void Seed(SqliteDbContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}
